package com.zhangtai.modules.service.impl.credential;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.CredentialHangDao;
import com.zhangtai.modules.dto.CredentialHangDto;
import com.zhangtai.modules.dto.CredentialInfoDto;
import com.zhangtai.modules.entity.credential.CredentialHangEntity;
import com.zhangtai.modules.service.credential.CredentialHangService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class CredentialHangServiceImpl extends ServiceImpl<CredentialHangDao, CredentialHangEntity> implements CredentialHangService {

    @Autowired
    private CredentialHangDao credentialHangDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<CredentialHangEntity> page = this.page(
                new Page<CredentialHangEntity>(pageNum, pageSize),
                new QueryWrapper<CredentialHangEntity>().like(!StringUtils.isEmpty(keyword), "name", keyword)
                        .eq("is_delete", 0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<CredentialHangDto> getHanglList(@RequestParam(value = "name", required = false) String name,
                                                      @RequestParam(value = "status", required = false) List<Integer> status,
                                                      @RequestParam(value = "orgIds", required = false) List<Long> orgIds,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        IPage<CredentialHangDto> page = new Page<>(pageNum, pageSize);
        List<CredentialHangDto> list = credentialHangDao.geHangInfoList(name, status, orgIds, page);
        page.setRecords(list);
        return new PageResult<>(page);
    }

    @Override
    public void unVaildHang(@RequestParam(value = "credentialId", required = false) Long credentialId) {
        credentialHangDao.unVaildHang(credentialId);
    }

    @Override
    public CredentialHangEntity getByIdE(Long id) {
        CredentialHangEntity one = this.getOne(new QueryWrapper<CredentialHangEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public CredentialHangEntity getByCredentialId(Long credentialId) {
        CredentialHangEntity one = this.getOne(new QueryWrapper<CredentialHangEntity>().eq("credential_id", credentialId).eq("is_delete", 0));
        return one;
    }


    @Override
    public R saveE(CredentialHangEntity entity) {
        boolean b = this.save(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(CredentialHangEntity entity) {
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
        CredentialHangEntity entity = new CredentialHangEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()) {
                return r;
            }
        }
        return R.ok();
    }

}